# Maintainer: Diego Sogari <diego.sogari@gmail.com>

_realname=binaryen
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=88
pkgrel=1
pkgdesc="Compiler infrastructure and toolchain library for WebAssembly, in C++ (mingw-w64)"
arch=('any')
url="https://github.com/WebAssembly/binaryen"
license=('Apache')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/WebAssembly/binaryen/archive/version_${pkgver}.tar.gz"
        001-install-fix.patch)
sha256sums=('32129819939596951eac63b2bad94606237cddccfc7c702e53c39c83841bb928'
            '7b73855ff4323d3c3cbea6f70e7a1d33f68502394c625d92e421dc80d15ef774')

prepare() {
  cd "${srcdir}/${_realname}-version_${pkgver}"
  patch -p1 -i ${srcdir}/001-install-fix.patch
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config} \
    "../${_realname}-version_${pkgver}"

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -Dm644 ${srcdir}/${_realname}-version_${pkgver}/LICENSE ${okgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE
}
